package com.sgb.kjwl.viewmodel.shopping;


import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.repository.ShoppingCarRepertory;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.fragment.shop.ShopTdChildFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ShopTdChildModel extends BaseViewModel {
    private ShopTdChildFragment fragment;
    private ShoppingCarRepertory repertory;

    public ShopTdChildModel(ShopTdChildFragment fragment, ShoppingCarRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    public void getH5Url(String id) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("collection").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {//type 0 首页 1 我的集采 2 我的报价
                    SharedPreferenceUtil.init(fragment.getActivity());
                    String token = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.resourceAPPToken, "");
                    String url = response.body().getData().getUrl() + "?id=" + id + "&type=0" + "&token=" + repertory.getToken();
//                    String url = "http://192.168.2.33:8067/collection" + "?id=" + id + "&type=0"+"&token="+ repertory.getToken();
                    WebViewX5HostActivity.start(fragment.getContext(), url);
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
